﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using aier.wx.nosql.redis;
using aier.xrm.wcf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class HomeBll
    {
        public RedisTestDal redisTestDal { get; set; }
        public HomeDal homeDal { get; set; }

        /// <summary>
        /// 根据负责人分页获取数据
        /// </summary>
        /// <param name="ownerId">负责人ID</param>
        /// <param name="currentPageIndex">当前页码</param>
        /// <param name="pageSize">页码大小</param>
        /// <returns></returns>
        public PageResult<HomeInfo> QueryHomeInfoByPagin(Guid ownerId, int currentPageIndex, int pageSize,string order)
        {
            //计算开始与结束位置
            //int endIndex = currentPageIndex * pageSize;
            int skipRecords = (currentPageIndex - 1) + pageSize;

            List<HomeInfo> homeInfos = homeDal.QueryHomeInfoByPaging(ownerId, skipRecords, pageSize + 1, order);
            PageResult<HomeInfo> pageResult = new PageResult<HomeInfo>();
            pageResult.existsnextdata = homeInfos.Count > pageSize;
            pageResult.total = homeDal.QueryHomeInfoTotal(ownerId);
            pageResult.datas = homeInfos.Skip(0).Take(pageSize).ToList();

            return pageResult;
        }

        public void setRedis(string key,string value)
        {
            redisTestDal.Set(key, value);
        }

        /// <summary>
        /// 根据负责人和搜索条件获取数据
        /// </summary>
        /// <param name="ownerId">负责人ID</param>
        /// <param name="condition">条件：手机号码，座机(模糊搜索)，姓名(模糊搜索)</param>
        /// <returns></returns>
        public PageResult<HomeInfo> QueryHomeInfoByCondition(Guid ownerId ,string condition)
        {
            List<HomeInfo> homeInfos = null;
            if (CommonUtil.IsMobile(condition))
            {
                //如果为手机
                homeInfos = homeDal.QueryHomeInfoByMobile(ownerId, condition);
            }
            else if (CommonUtil.IsContainChinese(condition))
            {
                //如果为姓名
                homeInfos = homeDal.QueryHomeInfoByName(ownerId, condition);
            }
            else
            {
                //否则为座机
                homeInfos = homeDal.QueryHomeInfoByTelephone(ownerId, condition);
            }            
            PageResult<HomeInfo> pageResult = new PageResult<HomeInfo>();
            pageResult.datas = homeInfos;
            pageResult.total = homeInfos.Count;

            return pageResult;
        }
        ///// <summary>
        ///// 验证是否是手机号码
        ///// </summary>
        ///// <param name="condition"></param>
        ///// <returns></returns>
        //public bool IsMobile(string condition)
        //{
        //    if (condition.Length != 11)
        //    {
        //        return false;
        //    }
        //    return System.Text.RegularExpressions.Regex.IsMatch(condition, @"^1(3|4|5|6|7|8|9)\d{9}");
        //}

        ///// <summary>
        ///// 判断是否为汉字或字母
        ///// </summary>
        ///// <param name="condition"></param>
        ///// <returns></returns>
        //public static bool IsContainChinese(string condition)
        //{
        //    if (System.Text.RegularExpressions.Regex.IsMatch(condition.ToString(), @"[\u4e00-\u9fbb]+")|| 
        //        System.Text.RegularExpressions.Regex.IsMatch(condition.ToString(), @"[a-zA-Z]"))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
    }
}
